package com.fone.player.activity.personal.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.entity.OfflineCache;

/**
 * 缓存管理 adapter (在我的缓存-->删除按钮激活后使用该adapter) 该界面下除对应itemClick可相应,其他空间相应均失效!
 * 
 * @author JasonZue
 * @since 2014-4-22
 */
public class CacheManageListAdatper extends BaseAdapter {

	private List<OfflineCache> mOfflineCacheList;
	private Context mContext;

	public CacheManageListAdatper(Context mContext) {
		this.mContext = mContext;
	}

	public void setOfflineCacheList(List<OfflineCache> offlineCacheList) {
		mOfflineCacheList = offlineCacheList;
	}

	@Override
	public int getCount() {
		return mOfflineCacheList != null ? mOfflineCacheList.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return mOfflineCacheList != null ? mOfflineCacheList.get(position)
				: null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext,
					R.layout.personal_cache_list_item, null);

			holder.selectedCb = (CheckBox) convertView
					.findViewById(R.id.cache_is_selected_cb);
			holder.downloadStatusTv = (TextView) convertView
					.findViewById(R.id.collection_listview_item_download_status_tv);
			holder.seasonInfo = (TextView) convertView
					.findViewById(R.id.collection_listview_item_season_tv);
			holder.collectionType = (TextView) convertView
					.findViewById(R.id.collection_listview_item_collection_type_tv);
			holder.downloadedSize = (TextView) convertView
					.findViewById(R.id.collection_listview_item_downloaded_tv);
			holder.totalSize = (TextView) convertView
					.findViewById(R.id.collection_listview_item_totle_progress_tv);
			holder.upDateInfo = (TextView) convertView
					.findViewById(R.id.collection_listview_item_update_info_tv);
			holder.itemImage = (ImageView) convertView
					.findViewById(R.id.collection_listview_item_pic_iv);
			holder.itemName = (TextView) convertView
					.findViewById(R.id.collection_listview_item_name_tv);
			holder.statusRl = (RelativeLayout) convertView
					.findViewById(R.id.collection_download_status_rl);
			holder.downloadProgressBar = (ProgressBar) convertView
					.findViewById(R.id.collection_listview_item_progressbar_pb);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// 设置checkbox显示颜色
		holder.selectedCb
				.setBackgroundResource(R.drawable.collection_delete_checkbox_selector);
		holder.selectedCb.setVisibility(View.VISIBLE);
		holder.selectedCb.setClickable(false);

		if (null != mOfflineCacheList.get(position)) {

			holder.itemName.setText(mOfflineCacheList.get(position)
					.getCacheName());
			holder.seasonInfo.setText(mOfflineCacheList.get(position)
					.getCacheEpisodeNum());
			switch (mOfflineCacheList.get(position).getCacheContentType()) {
			case OfflineCache.CACHE_CONTENT_TYPE_ANIMATION:
			case OfflineCache.CACHE_CONTENT_TYPE_TV:
			case OfflineCache.CACHE_CONTENT_TYPE_VARIETY:
				// 剧集类
				switch (mOfflineCacheList.get(position).getCacheHierarchyType()) {
				case OfflineCache.CACHE_HIERARCHY_FOLDER:
					holder.downloadedSize.setText(mOfflineCacheList.get(
							position).getCacheFinishCount()
							+ "集");
					holder.totalSize.setText("/"
							+ mOfflineCacheList.get(position)
									.getCacheAllCount() + "集");
					if (mOfflineCacheList.get(position).getCacheAllCount() != 0) {
						holder.downloadProgressBar
								.setProgress(mOfflineCacheList.get(position)
										.getCacheFinishCount()
										* 100
										/ mOfflineCacheList.get(position)
												.getCacheAllCount());
					} else {
						holder.downloadProgressBar.setProgress(0);
					}
					break;
				case OfflineCache.CACHE_HIERARCHY_FILE:
					if (mOfflineCacheList.get(position).getCacheAlreadySize() == 0) {
						holder.downloadedSize.setText("");
						holder.totalSize.setText("");
						holder.downloadProgressBar.setProgress(0);
					} else {

						holder.downloadedSize.setText(Formatter.formatFileSize(
								mContext, mOfflineCacheList.get(position)
										.getCacheAlreadySize()));
						holder.totalSize.setText("/"
								+ Formatter.formatFileSize(mContext,
										mOfflineCacheList.get(position)
												.getCacheTotalSize()));
						try {
							holder.downloadProgressBar
									.setProgress((int) (100 * mOfflineCacheList
											.get(position)
											.getCacheAlreadySize() / mOfflineCacheList
											.get(position).getCacheTotalSize()));
						} catch (Exception e) {
							e.printStackTrace();
							holder.downloadProgressBar.setProgress(0);
						}
					}
					break;
				}
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_LIVE_AUDIO:
			case OfflineCache.CACHE_CONTENT_TYPE_LIVE_VIDEO:
			case OfflineCache.CACHE_CONTENT_TYPE_MOVIE:
			case OfflineCache.CACHE_CONTENT_TYPE_NEWS:
			case OfflineCache.CACHE_CONTENT_TYPE_ORIGINAL:
			case OfflineCache.CACHE_CONTENT_TYPE_SHORT_FILM:
			case OfflineCache.CACHE_CONTENT_TYPE_OTHER:
				// 非剧集类
				if (mOfflineCacheList.get(position).getCacheAlreadySize() == 0) {
					holder.downloadedSize.setText("");
					holder.totalSize.setText("");
					holder.downloadProgressBar.setProgress(0);
				} else {
					holder.downloadedSize.setText(Formatter.formatFileSize(
							mContext, mOfflineCacheList.get(position)
									.getCacheAlreadySize()));
					holder.totalSize.setText("/"
							+ Formatter.formatFileSize(mContext,
									mOfflineCacheList.get(position)
											.getCacheTotalSize()));
					holder.downloadProgressBar
							.setProgress((int) (100 * mOfflineCacheList.get(
									position).getCacheAlreadySize() / mOfflineCacheList
									.get(position).getCacheTotalSize()));
				}
				break;
			default:
				break;

			}
			holder.upDateInfo.setText(mOfflineCacheList.get(position)
					.getCacheEpisodeUpdateInfo());
			holder.selectedCb.setChecked(mOfflineCacheList.get(position)
					.getCacheIsDelete());

			// 设置图片
			if (!TextUtils.isEmpty(mOfflineCacheList.get(position)
					.getCacheImageUrl())) {
				ImageDownloadModule.getInstance().download(
						mOfflineCacheList.get(position).getCacheImageUrl(),
						holder.itemImage);
			}

			// 修改图标及字体颜色
			holder.statusRl.setVisibility(View.GONE);
			holder.downloadStatusTv.setVisibility(View.GONE);
			switch (mOfflineCacheList.get(position).getCacheDownloadState()) {
			case OfflineCache.CACHE_STATE_WAITING:
			case OfflineCache.CACHE_STATE_QWAITING:
				changeStatus(holder, R.drawable.collect_icon_wait,
						R.color.cache_item_status_text_color, "等待中", true);
				break;
			case OfflineCache.CACHE_STATE_PAUSE_USER:
			case OfflineCache.CACHE_STATE_PAUSE:
				changeStatus(holder, R.drawable.collect_icon_pause,
						R.color.cache_item_status_text_color, "暂停", true);
				break;
			case OfflineCache.CACHE_STATE_DOWNLOADING:
				changeStatus(holder, R.drawable.collect_icon_download,
						R.color.cache_item_status_text_color, "下载中", true);
				break;
			case OfflineCache.CACHE_STATE_FINISH:
				changeStatus(holder, R.drawable.collect_icon_download,
						R.color.cache_item_green_color, "已完成", false);
				break;
			case OfflineCache.CACHE_STATE_ERROR:
				changeStatus(holder, R.drawable.collect_icon_wait,
						R.color.cache_item_status_text_color, "出错", true);
				break;
			case OfflineCache.CACHE_STATE_NOT_DOWNLOAD:
				changeStatus(holder, R.drawable.collect_icon_wait,
						R.color.cache_item_status_text_color, "", false);
				break;
			default:
				changeStatus(holder, R.drawable.collect_icon_wait,
						R.color.cache_item_status_text_color, "", false);
				break;
			}

			switch (mOfflineCacheList.get(position).getCacheContentType()) {
			case OfflineCache.CACHE_CONTENT_TYPE_TV:
				holder.collectionType.setText("电视剧");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_WEB_PAGE:
				holder.collectionType.setText("网页");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_MOVIE:
				holder.collectionType.setText("电影");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_ANIMATION:
				holder.collectionType.setText("动漫");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_SHORT_FILM:
				holder.collectionType.setText("短剧");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_NEWS:
				holder.collectionType.setText("新闻");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_LIVE_AUDIO:
				holder.collectionType.setText("直播音频");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_LIVE_VIDEO:
				holder.collectionType.setText("直播");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_ORIGINAL:
				holder.collectionType.setText("原创");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_VARIETY:
				holder.collectionType.setText("综艺");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_OTHER:
				holder.collectionType.setText("其他");
				break;
			default:
				holder.collectionType.setText("未知");
				break;
			}
		}

		holder.downloadStatusTv.setClickable(false);

		return convertView;
	}

	class ViewHolder {
		public TextView itemName, downloadStatusTv, seasonInfo, downloadedSize,
				totalSize, upDateInfo, collectionType;
		public ImageView itemImage, itemNew;
		public CheckBox selectedCb;
		public ProgressBar downloadProgressBar;
		public RelativeLayout statusRl;
	}

	/**
	 * 修改下载状态
	 * 
	 * @param holder
	 * @param drawableId
	 *            图片R文件
	 * @param textColor
	 *            下载文字颜色R文件
	 * @param statusText
	 *            下载文字内容 String格式
	 * @param isShow
	 *            是否显示图标
	 * @return void
	 * @throws
	 */
	private void changeStatus(final ViewHolder holder, int drawableId,
			int textColor, String statusText, boolean isShow) {
		Drawable drawable = mContext.getResources().getDrawable(drawableId);

		holder.downloadStatusTv.setText(statusText);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		if (isShow) {
			holder.downloadStatusTv.setCompoundDrawables(null, drawable, null,
					null);
		} else {
			holder.downloadStatusTv
					.setCompoundDrawables(null, null, null, null);
		}
		holder.downloadStatusTv.setTextColor(mContext.getResources().getColor(
				textColor));
	}

}
